package com.ruoyi.web.controller.shop.goods;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.shop.goods.domain.ShopGoods;
import com.ruoyi.shop.goods.service.IShopBrandService;
import com.ruoyi.shop.goods.service.IShopGoodsEvaluateService;
import com.ruoyi.shop.goods.service.IShopGoodsModelService;
import com.ruoyi.shop.goods.service.IShopGoodsService;
import com.ruoyi.shop.goods.vo.ShopGoodsEvaluateVo;
import com.ruoyi.shop.goods.vo.ShopGoodsVo;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;

/**
 * 商品Controller
 * 
 * |----------------------------------------------------------------------|
 * |favorshop [简单好用，让您喜爱的小商城]
 * |----------------------------------------------------------------------|
 * |copyright www.favorshop.cn All rights reserved.
 * |----------------------------------------------------------------------|
 * | @author xuxiang
 * |----------------------------------------------------------------------|
 * | @date 2023-05-29
 * |----------------------------------------------------------------------|
 */
@Controller
@RequestMapping("/shop/goods")
public class ShopGoodsController extends BaseController {
	private String prefix = "shop/goods/goods";

	@Autowired
	private IShopBrandService shopBrandService;
	@Autowired
	private IShopGoodsService shopGoodsService;
	@Autowired
	private IShopGoodsModelService shopGoodsModelService;
	@Autowired
	private IShopGoodsEvaluateService shopGoodsEvaluateService;

	@RequiresPermissions("shop:goods:view")
	@GetMapping()
	public String goods(ModelMap mmap) {
		return prefix + "/goods";
	}

	/**
	 * 查询商品列表
	 */
	@RequiresPermissions("shop:goods:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(ShopGoods goods) {
		startPage();
		List<ShopGoodsVo> list = shopGoodsService.selectGoodsList(goods);
		return getDataTable(list);
	}

	/**
	 * 导出商品列表
	 */
	@RequiresPermissions("shop:goods:export")
	@Log(title = "商品", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(ShopGoods goods) {
		List<ShopGoodsVo> list = shopGoodsService.selectGoodsList(goods);
		ExcelUtil<ShopGoodsVo> util = new ExcelUtil<ShopGoodsVo>(ShopGoodsVo.class);
		return util.exportExcel(list, "商品数据");
	}

	/**
	 * 新增商品页
	 */
	@GetMapping("/add")
	public String add(ModelMap mmap) {
		mmap.put("brands", shopBrandService.queryBrands());
		mmap.put("models", shopGoodsModelService.queryModels());
		return prefix + "/add";
	}

	/**
	 * 新增保存商品
	 */
	@RequiresPermissions("shop:goods:add")
	@Log(title = "商品", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(ShopGoodsVo goodsVo) {
		return toAjax(shopGoodsService.insertGoods(goodsVo));
	}

	/**
	 * 修改商品页
	 */
	@GetMapping("/edit/{goodsId}")
	public String edit(@PathVariable("goodsId") Long goodsId, ModelMap mmap) {
		ShopGoodsVo goods = shopGoodsService.selectGoodsById(goodsId);
		mmap.put("models", shopGoodsModelService.queryModels());
		String[] images = Convert.toStrArray(StringUtils.defaultString(goods.getImages()));
		mmap.put("image1", images[0]);
		mmap.put("image2", images.length >= 2 ? images[1] : "");
		mmap.put("image3", images.length >= 3 ? images[2] : "");
		mmap.put("image4", images.length >= 4 ? images[3] : "");
		mmap.put("goods", goods);
		mmap.put("brands", shopBrandService.queryBrands());
		mmap.put("dataJson", JSONUtil.toJsonStr(goods.getGoodsSkus()));
		return prefix + "/edit";
	}

	/**
	 * 修改保存商品
	 */
	@RequiresPermissions("shop:goods:edit")
	@Log(title = "商品", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(ShopGoodsVo goodsVo) {
		return toAjax(shopGoodsService.updateGoods(goodsVo));
	}

	@GetMapping("/detail/{goodsId}")
	public String detail(@PathVariable("goodsId") Long goodsId, ModelMap mmap) {
		ShopGoodsVo goods = shopGoodsService.selectGoodsById(goodsId);
		mmap.put("goods", goods);
		mmap.put("brands", shopBrandService.queryBrands());
		mmap.put("dataJson", JSONUtil.toJsonStr(goods.getGoodsSkus()));
		return prefix + "/detail";
	}

	/**
	 * 商品评价列表
	 * 
	 * @param goodsId
	 * @return
	 */
	@PostMapping("/evaluate/{goodsId}")
	@ResponseBody
	public TableDataInfo evaluate(@PathVariable("goodsId") Long goodsId) {
		startPage();
		List<ShopGoodsEvaluateVo> list = shopGoodsEvaluateService.selectEvaluateByGoodsId(goodsId);
		return getDataTable(list);
	}

	/**
	 * 删除商品
	 */
	@RequiresPermissions("shop:goods:remove")
	@Log(title = "商品", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(shopGoodsService.deleteGoodsByIds(ids));
	}

	/**
	 * 商品状态修改
	 */
	@Log(title = "商品管理", businessType = BusinessType.UPDATE)
	@RequiresPermissions("shop:goods:edit")
	@PostMapping("/changeStatus")
	@ResponseBody
	public AjaxResult changeStatus(ShopGoods shopGoods) {
		return toAjax(shopGoodsService.changeStatus(shopGoods));
	}

}
